using System;
using CurrencyConv.Core.RatesDownloader;
using CurrencyConv.Core.SettingsManager;
using InversionOfControl;

namespace CurrencyConv
{
	static class IoC
	{
		private static CompactContainer _container;
		private static bool _configured;
		public static void Configure()
		{
			_container = new CompactContainer { DefaultLifestyle = LifestyleType.Transient };

			_container.AddComponent(typeof(ISettingsManager), typeof(SettingsManager), LifestyleType.Singleton);
			_container.AddComponent(typeof(IRatesDownloader), typeof(RatesDownloader));
			_container.AddComponent(typeof(IRatesParser), typeof(RatesParser));
			_container.AddComponent(typeof(IWebUpdater), typeof(WebUpdater));
			_container.AddComponent(typeof(IContextPersister), typeof(ContextPersister));

			_configured = true;
		}

		public static T Resolve<T>()
		{
			if (!_configured)
				throw new Exception("The IoC container was not configured before use. Call Configure() on the IoC class before the Application.Run.");
			return _container.Resolve<T>();
		}
	}
}